package com.sxfq.loansupermarket.backsystem.modular.marketing.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.ExtenAccountInfo;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.marketing.dao.CallRecordsDao;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.ReservationsCallInfo;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.ReturnVInfo;
import com.sxfq.loansupermarket.backsystem.modular.marketing.service.ICallRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class CallRecordsServiceImpl implements ICallRecordsService {
    @Autowired
    private SqlMapper sqlMapper;

    @Autowired
    private CallRecordsDao dao;

    public List<ReservationsCallInfo> selectSecondByCallRecond(Page<ReservationsCallInfo> page, ReservationsCallInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getStartCreateTime())) {
            params.put("startCreateTime", searchInfo.getStartCreateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndCreateTime())) {
            params.put("endCreateTime", searchInfo.getEndCreateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getWanName())) {
            params.put("name", searchInfo.getWanName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getAreaAbbreviation())) {
            params.put("areaAbbreviation", searchInfo.getAreaAbbreviation());
        }
        if (StringUtil.isNotEmpty(searchInfo.getResult())) {
            params.put("result", searchInfo.getResult());
        }
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.getQueryPagesRecond(page, params);
    }

    public int getQueryPagesCount(Page<ReservationsCallInfo> page, ReservationsCallInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtil.isNotEmpty(searchInfo.getStartCreateTime())) {
            params.put("startCreateTime", searchInfo.getStartCreateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndCreateTime())) {
            params.put("endCreateTime", searchInfo.getEndCreateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getWanName())) {
            params.put("name", searchInfo.getWanName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getAreaAbbreviation())) {
            params.put("areaAbbreviation", searchInfo.getAreaAbbreviation());
        }
        if (StringUtil.isNotEmpty(searchInfo.getResult())) {
            params.put("result", searchInfo.getResult());
        }
        return dao.getQueryPagesCallCount(page, params);
    }

    @Override
    public List<ReservationsCallInfo> selectSecondByCallInRecond(Page<ReservationsCallInfo> page, ReservationsCallInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getStartCreateTime())) {
            params.put("startCreateTime", searchInfo.getStartCreateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndCreateTime())) {
            params.put("endCreateTime", searchInfo.getEndCreateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getWanName())) {
            params.put("name", searchInfo.getWanName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getAreaAbbreviation())) {
            params.put("areaAbbreviation", searchInfo.getAreaAbbreviation());
        }
        if (StringUtil.isNotEmpty(searchInfo.getResult())) {
            params.put("result", searchInfo.getResult());
        }
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.getQueryPagesInRecond(page, params);
    }

    @Override
    public int getQueryPagesInCount(Page<ReservationsCallInfo> page, ReservationsCallInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtil.isNotEmpty(searchInfo.getStartCreateTime())) {
            params.put("startCreateTime", searchInfo.getStartCreateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndCreateTime())) {
            params.put("endCreateTime", searchInfo.getEndCreateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getWanName())) {
            params.put("name", searchInfo.getWanName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getAreaAbbreviation())) {
            params.put("areaAbbreviation", searchInfo.getAreaAbbreviation());
        }
        if (StringUtil.isNotEmpty(searchInfo.getResult())) {
            params.put("result", searchInfo.getResult());
        }
        return dao.getQueryPagesCallInCount(page, params);
    }

    public List<ExtenAccountInfo> getArealist() {
        return dao.getArealist();
    }

    public List<ExtenAccountInfo> getWanNameList(String area) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtil.isNotEmpty(area)) {
            params.put("area", area);
        }
        return dao.getWanNameList(params);
    }

    public List<ReservationsCallInfo> selectGeCallRecond(String customerPhone) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtil.isNotEmpty(customerPhone)) {
            params.put("customerPhone", customerPhone);
        }
        return dao.getRecondlist(params);
    }

    public int handleMissed(ReservationsCallInfo reservationsCallInfo) {
        return dao.handleMissed(reservationsCallInfo);
    }

    public List<ReturnVInfo> returnVlist(String customerPhone){
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtil.isNotEmpty(customerPhone)) {
            params.put("customerPhone", customerPhone);
        }
        return dao.returnVlist(params);
    }

    public List<ReservationsCallInfo> selectCallRecondTotal(ReservationsCallInfo info) {
        String sql1 = " select t.id,t.name from reservemoney_call_record t  where t.create_time>='" + info.getStartCreateTime() + "' " +
                "and t.create_time<='" + info.getEndCreateTime() + "' and t.area_abbreviation ='" + info.getAreaAbbreviation() + "'  group by t.name";
        List<ReservationsCallInfo> list = sqlMapper.selectList(sql1, ReservationsCallInfo.class);
        String sql = "";
        if (list.size() <= 0) {
            return list;
        } else {
            for (int i = 0; i < list.size(); i++) {
                String classification = list.get(i).getName();
                String fenname = "'" + list.get(i).getName() + "' as name";
                sql += this.getCallTotal(info, fenname, classification, i, list, info.getAreaAbbreviation());
            }
            List<ReservationsCallInfo> list1 = sqlMapper.selectList(sql, ReservationsCallInfo.class);
            if (list1.size() <= 0) {
                return null;
            } else {
                return list1;
            }
        }
    }

    public String getCallTotal(ReservationsCallInfo info, String fenname, String classification,
                               int i, List<ReservationsCallInfo> list, String area) {
        String sql = " select " + fenname + " , " +
                " (select count(1) from (select t.id,t.customer_phone from reservemoney_call_record t where t.create_time>='" + info.getStartCreateTime()
                + "' and create_time <='" + info.getEndCreateTime() + "' and t.name='" + classification + "' and t.area_abbreviation='" + area + "')t) AS totalNumber," +

                " (select count(1) from (select t.id,t.customer_phone from reservemoney_call_record t where t.create_time>='" + info.getStartCreateTime() + "' " +
                "and create_time <='" + info.getEndCreateTime() + "' and t.name='" + classification + "' and t.is_effective=1 and t.area_abbreviation='" + area + "')t) AS effectiveNumber," +

                " (select count(1) from (select t.id,t.customer_phone from reservemoney_call_record t where t.create_time>='" + info.getStartCreateTime() + "' " +
                "and create_time <='" + info.getEndCreateTime() + "' and t.name='" + classification + "' and t.is_effective=2 and t.area_abbreviation='" + area + "')t) AS invalidNumber," +

                " (select count(1) from (select t.id,t.customer_phone from reservemoney_call_record t where EXISTS (SELECT a.customer_phone FROM distinction_customer_status a WHERE " +
                "t.customer_phone = a.customer_phone AND t.create_time >= '" + info.getStartCreateTime() + "' AND create_time <= '" + info.getEndCreateTime() + "' and t.name='" + classification + "'  and t.area_abbreviation='" + area + "'))t) AS arrivalNumber," +

                " (select SUM(t.totalHold) from (select t1.hold as totalHold from reservemoney_call_record t1 where  t1.create_time>='" + info.getStartCreateTime() + "' " +
                "and t1.create_time <='" + info.getEndCreateTime() + "' and t1.name='" + classification + "' and t1.area_abbreviation='" + area + "') t) as totalHoldS  ";
        if (i == list.size() - 1) {
        } else {
            sql += " union all ";
        }
        return sql;
    }

}